package com.advanced;

import org.springframework.jdbc.datasource.DataSourceUtils;

import java.lang.reflect.Array;
import java.util.*;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-29 14:26:49
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-29     张李鑫                     1.0         1.0 Version
 */
public class ThreeSum {

    public static List<List<Integer>> threeSum(int[] nums) {
        ArrayList<List<Integer>> lists = new ArrayList<>();

        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 2; i++) {
            if (nums[i] > 0) {
                break;
            }
            if (nums[i]==nums[i+1]&&nums[i]>0){
                continue;
            }
            int right = nums.length - 1;
            int left = i + 1;
            while (right > left) {
                if (nums[i] + nums[right] + nums[left] > 0) {
                    right--;
                } else if (nums[i] + nums[right] + nums[left] < 0) {
                    left++;
                } else {
                    ArrayList<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[left]);
                    list.add(nums[right]);
                    lists.add(list);
                    right--;
                    left++;

                }
            }
        }
        return lists;
    }

    /**
     * //                if (nums[i] + nums[right] + nums[left] == 0) {
     * //                    ArrayList<Integer> list = new ArrayList<>();
     * //                    list.add(nums[i]);
     * //                    list.add(nums[left]);
     * //                    list.add(nums[right]);
     * //                    lists.add(list);
     * //                    right--;
     * //                    left++;
     * //                    continue;
     * //                }
     *
     * @param args
     */

    public static void main(String[] args) {
        int[] nums = new int[]{-1, 0, 1, 2, -1, -4};
        List<List<Integer>> lists = threeSum(nums);
        System.out.println(lists);
    }
}
